package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.service.UserService;
import com.jt.util.ObjectMapperUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/user")
public class UserController {


    //消费者启动时,不需要检查是否有服务的提供者
  //  @Reference(check = false)
   // private DubboUserService userService;
    //@Autowired
   // private UserService userService;

    @Autowired
    private UserService userService;
    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("/getMsg")
    public String getMsg(){
        return "单点登录系统搭建完成!!!";
    }

    /**
     * 1.url:http://sso.jt.com/user/check/{param}/{type}
     * 2.参数：params 需要校验的数据
     *        type  校验的类型
     * 3.返回值结果：  SysResult对象  data:true/false
     * 4.jsonp跨域访问
     * 终极目标： 1.快  省  安全
     * @param param
     * @param type
     * @param callback
     * @return
     */
    @RequestMapping("/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param,@PathVariable Integer type,
                                    String callback){

        boolean flag = userService.checkUser(param,type);
        SysResult systeSult = SysResult.success(flag);
        return new JSONPObject(callback,systeSult);
    }

    /**
     * 利用jsonp获取用户信息
     *url:http://sso.jt.com/user/query/826f4938c2a544edb7282139a35ddf56?callback=jsonp1595583195159&_=1595583195203
     */

    /**
     * 业务接口说明
     * 1.url网址：http://sso.jt.com/user/query/{ticket}?callback=jsonp18593
     * 2.参数：ticket信息
     * 3.返回值结果：SysResult对象{userJSON}信息
     * 业务逻辑说明：根据ticket信息，查询redis获取userJSON信息
     * @param callback
     * @param ticket
     * @return
     */
    @RequestMapping("/query/{ticket}")
    public JSONPObject findUserByTicket(String callback,@PathVariable String ticket){
        if (jedisCluster.exists(ticket)){
            //用户之前登陆过
            String data = jedisCluster.get(ticket);
            System.out.println(data);
            SysResult sysResult = SysResult.success(data);
            return new JSONPObject(callback,sysResult);
        }else{
            //用户信息有误
            SysResult sysResult = SysResult.fail();
            return new JSONPObject(callback,sysResult);
        }
    }


}


















